//
//  ELTileScrollViewModel.swift
//  WZApp
//
//  Created by evol on 2018/7/11.
//  Copyright © 2018年 evol. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

enum TabbarResponse {
    case cancel
    case fistLevel(id:NSInteger)
    case sencondLevel(id:NSInteger)
}

class ELTileScrollViewModel {
    let disposeBag = DisposeBag()

    var tabbarResponse = PublishSubject<TabbarResponse>() //过滤分类
    
    var classifiedAnnotations = BehaviorRelay<[ELTagModel]>(value: [])  // 分类过滤的
    
    var annotationTapCommand = PublishSubject<ELTagModel>() // 点击页签
    
    init() {
        AppCommonData.share.tagModels.flatMap(filterResult)
        .subscribeOn(ConcurrentDispatchQueueScheduler(qos: DispatchQoS.userInitiated)) //后台线程序列
        .observeOn(MainScheduler.instance) //主线程监听
        .subscribe(onNext:{
           [weak self] result in
            self?.classifiedAnnotations.accept(result)
        }).disposed(by: disposeBag)
    }
    
    func filterResult(data:[ELTagModel])-> Observable<[ELTagModel]> {
            return tabbarResponse
                .flatMapLatest{
                    query -> Observable<[ELTagModel]> in
                    var newData:[ELTagModel] = []
                    switch query {
                    case .cancel:
                        return Observable.just([])
                    case .fistLevel(let id):
                        for model in data {
                            if model.type == id {
                                newData.append(model)
                            }
                        }
                    case .sencondLevel(let id):
                        for model in data {
                            if model.subtype == id {
                                newData.append(model)
                            }
                        }
                    }
                    return Observable.just(newData)
        }
    }

}
